//
//  ForgetPwdViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/4/8.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "InputTwoView.h"
#import "GetCodeView.h"
@interface ForgetPwdViewController ()

/** 原密码 */
@property (nonatomic,strong) UITextField *orginPwdTF;
/** 新密码 */
@property (nonatomic,strong) UITextField *newsPwdTF;
/** 确认新密码 */
@property (nonatomic,strong) UITextField *surePwdTF;

@end

@implementation ForgetPwdViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    [self setUpUI];
}

- (void)setUpUI
{
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStylePlain target:self action:@selector(clickMethod:)];
    leftBarItem.tag = 1001;
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    scrollView.backgroundColor = WB_RGBColor(240, 240, 240);
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [scrollView addGestureRecognizer:singleTap];
    [self.view addSubview:scrollView];
    
    //手机号码
    InputTwoView *phoneView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    phoneView.titleLb.text = @"手机号";
    phoneView.inputTF.placeholder = @"请输入手机号码";
    phoneView.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    phoneView.frame = CGRectMake(0, 10, kScreenWidth, 45);
    phoneView.inputTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:AppUserPhone];
    [scrollView addSubview:phoneView];
    
    //验证码
    GetCodeView *getCodeView = [[NSBundle mainBundle] loadNibNamed:@"GetCodeView" owner:self options:nil].firstObject;
    getCodeView.titleLb.text = @"验证码";
    getCodeView.frame = CGRectMake(0, phoneView.mm_bottom, kScreenWidth, 45);
    getCodeView.inputTF.placeholder = @"请输入验证码";
    getCodeView.inputTF.secureTextEntry = YES;
//    _orginPwdTF = getCodeView.inputTF;
    [scrollView addSubview:getCodeView];
    
    //新密码
    InputTwoView *newPwdView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    newPwdView.titleLb.text = @"新密码";
    newPwdView.frame = CGRectMake(0, getCodeView.mm_bottom, kScreenWidth, 45);
    newPwdView.inputTF.secureTextEntry = YES;
    newPwdView.inputTF.placeholder = @"请填写新密码";
    _newsPwdTF = newPwdView.inputTF;
    [scrollView addSubview:newPwdView];
    
    //确认密码
    InputTwoView *surePwdView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    surePwdView.titleLb.text = @"确认密码";
    surePwdView.inputTF.secureTextEntry = YES;
    surePwdView.frame = CGRectMake(0, newPwdView.mm_bottom, kScreenWidth, 45);
    surePwdView.inputTF.placeholder = @"请再次确认密码";
    _surePwdTF = surePwdView.inputTF;
    [scrollView addSubview:surePwdView];
    
    //确定
    UIButton *sureBtn = [Factory customButtonWithTitle:@"确定"];
    sureBtn.tag = 1000;
    sureBtn.frame = CGRectMake(35, surePwdView.mm_bottom + 25, kScreenWidth - 70, 60);
    [sureBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:sureBtn];
}

- (void)clickMethod:(UIButton *)sender
{
    if (sender.tag == 1000) {
        WB_WeakSelf(self)
        if (WB_IsNilString(_orginPwdTF.text)) {
            [NSObject wb_showAlertWithTitle:@"请输入密码" duration:TipTime];
            return;
        }else if (WB_IsNilString(_newsPwdTF.text)){
            [NSObject wb_showAlertWithTitle:@"请输入新密码" duration:TipTime];
            return;
        }else if (WB_IsNilString(_surePwdTF.text)){
            [NSObject wb_showAlertWithTitle:@"请再次输入新密码" duration:TipTime];
            return;
        }else if ([_orginPwdTF.text isEqualToString:_newsPwdTF.text]){
            [NSObject wb_showAlertWithTitle:@"原密码不能与新密码相同" duration:TipTime];
            return;
        }else if (_orginPwdTF.text.length < 6 || _newsPwdTF.text.length < 6 || _surePwdTF.text.length < 6){
            [NSObject wb_showAlertWithTitle:@"密码必须大于6位" duration:TipTime];
            return;
        }else if (![_surePwdTF.text isEqualToString:_newsPwdTF.text]){
            [NSObject wb_showAlertWithTitle:@"两次输入的密码不一致" duration:TipTime];
            return;
        }
        
        [XYHttpRequest dataRequestWithNewPwd:_newsPwdTF.text oldPwd:_orginPwdTF.text success:^(id responseObj) {
            [NSObject wb_showAlertWithTitle:responseObj[@"info"] duration:1];
            if (WB_IsEquallString(responseObj[@"responseCode"], @"1")) {
                //延时1s返回
                WB_DISPATCH_MAIN_Delay_THREAD(1, ^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                })
            }
        } failure:^(NSError *error) {
            
        }];
    }else if (sender.tag == 1001){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    UIScrollView *scrollView = (UIScrollView *)gestureRecognizer.view;
    [scrollView endEditing:YES];
}

@end
